package cn.lopr.im.android.mvp.view.activities;

import android.annotation.SuppressLint;
import android.content.Context;
import android.content.Intent;
import android.os.Build;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.view.inputmethod.InputMethodManager;

import androidx.annotation.NonNull;
import androidx.annotation.RequiresApi;
import androidx.recyclerview.widget.RecyclerView;

import java.util.List;

import cn.lopr.im.android.R;
import cn.lopr.im.android.bean.ArticleCommentVo;
import cn.lopr.im.android.bean.ArticleVo;
import cn.lopr.im.android.bean.User;
import cn.lopr.im.android.common.app.Activity;
import cn.lopr.im.android.databinding.ActivityArticleViewBinding;
import cn.lopr.im.android.mvp.contact.IArticleViewContact;
import cn.lopr.im.android.mvp.presenter.ArticleViewPresenterImpl;
import cn.lopr.im.android.mvp.view.adapters.CommunityArticleCommentAdapter;
import cn.lopr.im.android.utils.DateUtil;

public class ArticleViewActivity extends Activity implements IArticleViewContact.IArticleViewView {
    private ActivityArticleViewBinding binding;
    private IArticleViewContact.IArticleViewPresenter articleViewPresenter;
    private CommunityArticleCommentAdapter communityArticleCommentAdapter;
    private String TAG;
    private String articleId;
    private User replyCommentUser;
    private String replyCommentId;

    @Override
    protected View getContentLayoutId() {
        binding = ActivityArticleViewBinding.inflate(getLayoutInflater());
        return binding.getRoot();
    }

    @Override
    protected void initData() {
        super.initData();
        articleId = getIntent().getStringExtra("articleId");

        articleViewPresenter = new ArticleViewPresenterImpl(this);
        articleViewPresenter.getArticle(this.getToken(this.getApplicationContext()));

        articleViewPresenter.getComment(this.getToken(this.getApplicationContext()), 1, 20);
    }

    @Override
    protected void initWidget() {
        super.initWidget();

        this.binding.layoutInput.tvSend.setOnClickListener(e -> this.articleViewPresenter.addComment(this.getToken(this.getApplicationContext())));
        this.binding.includeAppbar.ivBack.setOnClickListener(e -> this.finish());
        this.binding.includeAppbar.tvTitle.setText("动态详情");
        binding.getRoot().addOnLayoutChangeListener((v, left, top, right, bottom, oldLeft, oldTop, oldRight, oldBottom) -> {
            if (oldBottom != 0 && bottom != 0 && (oldBottom - bottom > 0)) {
                Log.i(TAG, "onLayoutChange: " + "键盘弹起");
            } else if (oldBottom != 0 && bottom != 0 && (bottom - oldBottom > 0)) {
                Log.i(TAG, "onLayoutChange: " + "键盘收起");
                replyCommentUser = null;
                replyCommentId = null;
            }
        });
        binding.includeAppbar.ivEdit.setOnClickListener(e -> {
            Intent intent = new Intent(ArticleViewActivity.this, ArticlePublishActivity.class);
            intent.putExtra("action", "update");
            intent.putExtra("articleId", articleId);
            startActivity(intent);
        });
    }

    @Override
    public void getCommentSuccess(List<ArticleCommentVo> data) {
        this.communityArticleCommentAdapter = new CommunityArticleCommentAdapter(this.getApplicationContext(), data);
        this.binding.communityArticleCommentList.setAdapter(this.communityArticleCommentAdapter);
        this.communityArticleCommentAdapter.notifyDataSetChanged();
        this.communityArticleCommentAdapter.onRecyclerViewItemClickListener(new CommunityArticleCommentAdapter.OnRecyclerViewItemClickListener() {
            @Override
            public void onItemClick(View view, int section) {
                Log.i(TAG, "onItemClick: " + view.getId());
                ArticleCommentVo articleCommentVo = (ArticleCommentVo) view.getTag();
                if (view.getId() == R.id.ll_publish_comment_user) {
                    Intent intent = new Intent(ArticleViewActivity.this, InfoActivity.class);
                    intent.putExtra("user", articleCommentVo.getCurrentUser());
                    startActivity(intent);
                } else if (view.getId() == R.id.iv_reply_comment) {
                    Log.i(TAG, "onItemClick: iv_reply_comment " + view.getId());
                    replyCommentUser = articleCommentVo.getCurrentUser();
                    replyCommentId = articleCommentVo.getId();
                    openInput();
                } else if (view.getId() == R.id.tv_reply_all) {
                    Intent intent = new Intent(getApplicationContext(), CommentDetailActivity.class);
                    intent.putExtra("commentId", articleCommentVo.getId());
                    startActivity(intent);
                }
            }

            @Override
            public void onItemLongClick(View view, int section) {

            }
        });
        this.binding.communityArticleCommentList.addOnScrollListener(new RecyclerView.OnScrollListener() {
            @Override
            public void onScrollStateChanged(@NonNull RecyclerView recyclerView, int newState) {
                super.onScrollStateChanged(recyclerView, newState);
            }

            @Override
            public void onScrolled(@NonNull RecyclerView recyclerView, int dx, int dy) {
                super.onScrolled(recyclerView, dx, dy);
            }
        });
    }

    @RequiresApi(api = Build.VERSION_CODES.M)
    @Override
    public void addCommentSuccess() {
        closeInput();
        binding.layoutInput.etContent.setText("");
        articleViewPresenter.getComment(this.getToken(this.getApplicationContext()), 1, 20);
        articleViewPresenter.getArticle(this.getToken(this.getApplicationContext()));
    }

    @SuppressLint("SetTextI18n")
    @Override
    public void getArticleSuccess(ArticleVo articleVo) {

        binding.includeCommunityArticle.tvContent.setText(articleVo.getContent());
        binding.includeCommunityArticle.tvPublishTime.setText(DateUtil.formatHMS(articleVo.getPublishTime()));
        binding.includeCommunityArticle.tvNickname.setText(articleVo.getNickname());

        binding.includeAppbar.ivEdit.setVisibility(articleVo.isSelf() ? View.VISIBLE : View.GONE);

        if (articleVo.isSelfLike()) {
            binding.includeCommunityArticle.ivLike.setImageResource(R.drawable.ic_yes_click);
        }

        if (articleVo.isSelfVisit()) {
            binding.includeCommunityArticle.ivVisit.setImageResource(R.drawable.ic_visit_click);

        }

        if (articleVo.isSelfComment()) {
            binding.includeCommunityArticle.ivComment.setImageResource(R.drawable.ic_comment_2_click);
        }

        if (articleVo.getVisits() == 0) {
            binding.includeCommunityArticle.tvVisit.setVisibility(View.GONE);
        } else {
            binding.includeCommunityArticle.tvVisit.setText(articleVo.getVisits().toString());
        }
        if (articleVo.getComment() == 0) {
            binding.includeCommunityArticle.tvComment.setVisibility(View.GONE);
        } else {
            binding.includeCommunityArticle.tvComment.setText(articleVo.getComment().toString());
        }
        if (articleVo.getLike() == 0) {
            binding.includeCommunityArticle.tvLike.setVisibility(View.GONE);
        } else {
            binding.includeCommunityArticle.tvLike.setText(articleVo.getLike().toString());
        }
    }

    @Override
    public String getComment() {
        return binding.layoutInput.etContent.getText().toString();
    }

    @Override
    public String getArticleId() {
        return articleId;
    }

    @Override
    public Integer getUserId() {
        return MainActivity.user.getId();
    }

    @Override
    public String getReplyCommentId() {
        return replyCommentId;
    }

    @RequiresApi(api = Build.VERSION_CODES.M)
    public void closeInput() {
        InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
        imm.hideSoftInputFromWindow(binding.layoutInput.etContent.getWindowToken(), 0);
        binding.layoutInput.etContent.setHint("   写评论...");
    }

    public void openInput() {
        binding.layoutInput.etContent.setFocusable(true);
        binding.layoutInput.etContent.setFocusableInTouchMode(true);
        //请求获得焦点
        binding.layoutInput.etContent.requestFocus();
        InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
        imm.showSoftInput(binding.layoutInput.etContent, 0);
        if (!TextUtils.isEmpty(replyCommentId)) {
            binding.layoutInput.etContent.setHint("   回复" + this.replyCommentUser.getNickname());
        }

    }

    @Override
    public void onResume() {
        super.onResume();

        if (articleViewPresenter != null) {
            articleViewPresenter.getComment(this.getToken(this.getApplicationContext()), 1, 20);
            articleViewPresenter.getArticle(this.getToken(this.getApplicationContext()));
        }
    }
}